package edu.sdibt.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.sdibt.bean.Student;
import edu.sdibt.bean.Student1;
import edu.sdibt.student.dao.StudentDAO;
import edu.sdibt.student.dao.impl.StudentDAOImpl;

/**
 * Servlet implementation class PersonServlet
 */
@WebServlet("*.do")
public class PersonServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    StudentDAO studentDAO=new StudentDAOImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PersonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		String method=request.getParameter("method");
//		switch (method) {
//		case "select":
//			select(request,response);
//			break;
//		case "add":
//			add(request,response);
//			break;
//		case "delete":
//			delete(request,response);
//			break;
//
//		default:
//			break;
//		}
		String url=request.getServletPath();
		String method=url.substring(1);
		String methodName=method.substring(0, method.length()-3);
	    try {
			Method m=getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			m.invoke(this, request,response); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		studentDAO.StuDelById(id);
		response.sendRedirect("select.do");
//		System.out.println(id);
	}
	
	private void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
	    String id=request.getParameter("id");
	    String name=request.getParameter("name");
	    String password=request.getParameter("password");
	    Student student=new Student(id, name, password);
	    int row=studentDAO.CheckStuName(name);
	    if(row==0) {
	    	int row1 =studentDAO.AddStu(student);
	    	if(row1==0) {
	    		response.sendRedirect("error.jsp");
	    	}else {
	    		response.sendRedirect("succes.jsp");
	    	}
	    }else {
	    	request.setAttribute("massage","用户名"+name+"已存在！请重新输入");
	    	request.getRequestDispatcher("/addstudent.jsp").forward(request, response);
	    }
	}
	
	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		List<Student> list=studentDAO.StudentSelectAll();
//		request.setAttribute("stugetall", list);
//		request.getRequestDispatcher("/index.jsp").forward(request, response);
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		Student1 student1=new Student1(id, name, password);
		List<Student> list=studentDAO.StuSelectByMoHu(student1);
		request.setAttribute("s", list);
		request.getRequestDispatcher("/index.jsp").forward(request, response);
//		System.out.println(1);
	}
	private void edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String path="/error1.jsp";
		String id=request.getParameter("id");
		Student student=studentDAO.selectById(id);
		if(student!=null) {
			path="/updatestudent.jsp";
			request.setAttribute("oldstu", student);
		}
//		System.out.println(student);
		request.getRequestDispatcher(path).forward(request, response);
	}
	private void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		String oldName=request.getParameter("oldName");
		if(!oldName.equals(name)) {
			int row1=studentDAO.CheckStuName(name);
			if(row1!=0) {
				Student student=studentDAO.selectById(id);
				request.setAttribute("oldstu",student);
				request.setAttribute("massage", "用户名"+name+"已存在！请重新输入");
				request.getRequestDispatcher("/updatestudent.jsp").forward(request, response);
			}else {
				int row=studentDAO.update(new Student(id, name, password));
				if(row!=0) {
					response.sendRedirect("select.do");
				}
				else {
					response.sendRedirect("error.jsp");
				}
			}
		}else {
			int row=studentDAO.update(new Student(id, name, password));
			if(row!=0) {
				response.sendRedirect("select.do");
			}
			else {
				response.sendRedirect("error.jsp");
			}
		}
		
	}
}
